package com.example.module_filemanage.MVVM.Module.QiNiu;

import android.util.Log;

import com.qiniu.util.Auth;
import com.qiniu.util.StringUtils;
import com.qiniu.util.UrlSafeBase64;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;

import static android.content.ContentValues.TAG;
import static com.example.module_filemanage.BuildConfig.AK;
import static com.example.module_filemanage.BuildConfig.SK;

/**
 * Created by SJ on 2018/4/18.
 */

public class TokenManager {

    public static TokenManager getInstance() {
        return SingletonHolder.INSTANCE;
    }

    private static class SingletonHolder {
        private static final TokenManager INSTANCE=new TokenManager();
    }
    /**
     * 获取管理凭证
     */
    public String getManageToken(String url){

        String ACCESS_KEY=AK;
        String SECRET_KEY=SK;
        //删除方法
        String signingStr=url;
        String t2="";
        try {
            Mac mac=javax.crypto.Mac.getInstance("HmacSHA1");
            mac.init(new SecretKeySpec(StringUtils.utf8Bytes(SECRET_KEY), "HmacSHA1"));

            t2=UrlSafeBase64.encodeToString(mac.doFinal(StringUtils.utf8Bytes(signingStr)));
            Log.e(TAG, "getManageToken||"+url+": "+ACCESS_KEY + ":" + t2 );
        }catch (Exception e){

        }
        return "QBox "+ACCESS_KEY + ":" + t2;
        }
    public String getUpLoadManageToken(String _bucket,String _key){
        String ACCESS_KEY=AK;
        String SECRET_KEY=SK;
        String bucket = _bucket;
      //  String key = _key;
        Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);
        String upToken = auth.uploadToken(bucket);
       return upToken;
    }

}
